import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";

export const routes = [
  {
    path: "/",
    redirect: "/login",
  },
  {
    path: "/home",
    name: "home",
    component: HomeView,
    meta: {
      name: "首页",
    },
    children: [
      {
        path: "manage",
        name: "manage",
        label: "账户管理",
        meta: {
          name: "账户管理",
        },
        component: () => import("../views/user/UserView.vue"),
        children: [
          {
            path: "managelist",
            name: "manageList",
            label: "管理员列表",
            meta: {
              name: "管理员列表",
            },
            component: () => import("../views/user/UserList.vue"),
          },
        ],
      },
      {
        path: "swipemanage",
        name: "swipemanage",
        label: "轮播图管理",
        meta: {
          name: "轮播图管理",
        },
        component: () => import("../views/swipe/SwipeView.vue"),
        children: [
          {
            path: "swipelist",
            name: "swipeList",
            label: "轮播图列表",
            meta: {
              name: "轮播图列表",
            },
            component: () => import("../views/swipe/SwipeList.vue"),
          },
          {
            path: "swipeAdd",
            name: "swipeAdd",
            label: "添加轮播图",
            meta: {
              name: "添加轮播图",
            },
            component: () => import("../views/swipe/SwipeAdd.vue"),
          },
        ],
      },
      {
        path: "product",
        name: "product",
        label: "产品管理",
        meta: {
          name: "产品管理",
        },
        component: () => import("../views/product/ProductViews.vue"),
        children: [
          {
            path: "ProductList",
            name: "ProductList",
            label: "产品列表",
            meta: {
              name: "产品列表",
            },
            component: () => import("../views/product/ProductList.vue"),
          },
          {
            path: "SecondProduct",
            name: "SecondProduct",
            label: "产品秒杀",
            meta: {
              name: "产品秒杀",
            },
            component: () => import("../views/product/SecondProduct.vue"),
          },
          {
            path: "RecommendList",
            name: "RecommendList",
            label: "推荐列表",
            meta: {
              name: "推荐列表",
            },
            component: () => import("../views/product/RecommendList.vue"),
          },
          {
            path: "ScreeningList",
            name: "ScreeningList",
            label: "筛选列表",
            meta: {
              name: "筛选列表",
            },
            component: () => import("../views/product/ScreeningList.vue"),
          },
        ],
      },
      {
        path: "echarts",
        name: "echarts",
        label: "数据可视化",
        meta: {
          name: "数据可视化",
        },
        component: () => import("../views/echarts/EchartsView.vue"),
        children: [
          {
            path: "echartsshow",
            name: "echartsshow",
            label: "Echarts数据展示",
            meta: {
              name: "Echarts数据展示",
            },
            component: () => import("../views/echarts/EchartsShow.vue"),
          },
          {
            path: "echartsUother",
            name: "echartsUother",
            label: "Echarts其他数据",
            meta: {
              name: "Echarts其他数据",
            },
            component: () => import("../views/echarts/EchartsUother.vue"),
          },
        ],
      },
      {
        path: "editor",
        name: "editor",
        label: "编辑器",
        meta: {
          name: "编辑器",
        },
        component: () => import("../views/editor/EditorView.vue"),
        children: [
          {
            path: "editordome",
            name: "editordome",
            label: "富文本编辑器",
            meta: {
              name: "富文本编辑器",
            },
            component: () => import("../views/editor/EditorDemo.vue"),
          },
        ],
      },

      {
        path: "map",
        name: "map",
        label: "地图",
        meta: {
          name: "地图",
        },
        component: () => import("../views/map/mapView.vue"),
        children: [
          {
            path: "Map",
            name: "Map",
            label: "我的地图",
            meta: {
              name: "我的地图",
            },
            component: () => import("../views/map/myMap.vue"),
          },
        ],
      },
    ],
  },
  {
    path: "/login",
    name: "login",
    component: () => import("../views/login.vue"),
  },
  {
    path: "/about",
    name: "about",
    component: () => import("../views/AboutView.vue"),
  },
];
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
});
// 防跳墙
router.beforeEach((to, from) => {
  if (to.fullPath === "/login") {
    if (localStorage.getItem("token")) return { path: "/home" };
    return true;
  } else {
    if (localStorage.getItem("token")) {
      return true;
    } else {
      return { path: "/login" };
    }
  }
});
export default router;
